package com.cxdr.awsips.distribute.bean;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.cxdr.awsips.common.logger.LogFactory;
import com.cxdr.awsips.distribute.config.LinkConfig;
import com.cxdr.awsips.distribute.pool.UnCompletePool;

/**
 * 未完成的分发文件,用于监控各个目标的分发是否完成
 * 
 * @author pugang
 * 
 */
public class UnCompleteFile {
	private static Logger logger = LogFactory.makeNewLogger(UnCompleteFile.class);
	private String fileName;
	private LinkConfig link;
	private String sourceID;
	private Map<String, String> targetResults =  new HashMap<String, String>();

	public UnCompleteFile(LinkConfig link) {
		this.link = link;
	}

	public UnCompleteFile(String fileName,String sourceID) {
		this.fileName = fileName;
		this.sourceID = sourceID;
	}

	public synchronized void addTarget(String targetID, String result) {
		targetResults.put(targetID, result);
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}


	public boolean isComplete() {
		if(link==null){
			return false;
		}
		return targetResults.size() == link.getTargetSize();
	}
	public int getCompleteCount(){
		return targetResults.size();
	}
	public Map<String, String> getTargetResults() {
		return targetResults;
	}

	public void setTargetResults(Map<String, String> targetResults) {
		this.targetResults = targetResults;
	}
	/**
	 * 只要满足文件明相同即认为相等
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj != null) {
			if (this == obj) {
				return true;
			}
			if (obj instanceof UnCompleteFile) {
				if (this.fileName != null) {
					return this.fileName
							.equalsIgnoreCase(((UnCompleteFile) obj)
									.getFileName());
				}
			}

		}
		return false;
	}
}
